import { Service } from 'egg';

export default class ProjectService extends Service {

    /**
     * 创建项目
     * @param param 
     */
    public async createProject(param: any) {
        try {
            //创建主键
            param.project_id = this.ctx.helper.genUUID();
            param.is_delete = '0';
            const result: any = await this.ctx.model.Project.create(param);
            if (!result) {
                throw new Error(JSON.stringify({ code: 2, msg: '创建失败' }));
            }
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 删除项目
     * @param param 
     */
    public async deleteProject(param: any) {
        try {
            const result: any = await this.ctx.model.Project.update({ is_delete: '1' }, { where: { project_id: param.project_id } });
            if (result[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '删除失败' }));
            }
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 修改项目
     * @param param 
     */
    public async updateProject(param: any) {
        try {
            const { project_id, project_name, project_picture, project_content } = param;
            const result: any = await this.ctx.model.Project.update({ project_name, project_picture, project_content }, { where: { project_id, is_delete: '0' } });
            if (result[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '修改失败' }));
            }
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 查找项目
     * @param param 
     */
    public async searchProject(param: any) {
        try {
            let { project_name } = param;
            const actulParam = {};
            let queryStr = `select project_id,project_name,project_picture,project_content from t_project where is_delete='0'`;
            if (project_name) {
                queryStr += ` and project_name like :project_name`;
                actulParam['project_name'] = '%' + project_name + '%';
            }
            queryStr += ` order by created_at desc`;
            const result: any = await this.ctx.model.query(queryStr, {
                type: this.ctx.model.QueryTypes.SELECT,
                replacements: actulParam
            })
            if (result.length <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
            }
            return result;
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 根据Id查找项目详情
     * @param param 
     */
    public async searchProjectById(param:any){
        try{
            const result:any=await this.ctx.model.Project.findOne({attributes:['project_name','project_picture','project_content'],where:{project_id:param.project_id,is_delete:'0'}});
            if(!result){
                throw new Error(JSON.stringify({code:2,msg:'查找失败'}));
            }
            return result;
        }catch(e){
            this.logger.error(e);
            throw new Error(e.message);
        }
    }
}